package jp.co.brilliantservice.app.sos;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class Main extends MapActivity {
    private String mTemplateTweetMes;
    private String mLat;
    private String mLon;
    private SettingManager mPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        setContentView(R.layout.main_layout);

        mPref = new SettingManager(this);

        // 名前テキスト
        EditText edit = (EditText) findViewById(R.id.main_txt_name);
        edit.setText(mPref.get("name"));
        edit.addTextChangedListener(textWatcher);

        initMyLocation();
    }

    private void initMyLocation() {
        MapView map = new MapView(this, "0KEAGagan9L9fdCnYbTJ1ro9I4nUNlSvQCpsgYg");
        final MyLocationOverlay marker = new MyLocationOverlay(this, map);
        marker.enableMyLocation();
        marker.enableCompass();
        marker.runOnFirstFix(new Runnable() {
            public void run() {
                GeoPoint p = marker.getMyLocation();
                double lat = Integer.valueOf(p.getLatitudeE6()).doubleValue() / 1e6;
                mLat = Double.toString(lat);
                double lng = Integer.valueOf(p.getLongitudeE6()).doubleValue() / 1e6;
                mLon = Double.toString(lng);
                Location loc = marker.getLastFix();
                Log.i("SOS", "lat=" + mLat + ", lon=" + mLon + ", Accuracy=" + loc.getAccuracy());
            }
        });
        map.getOverlays().add(marker);
    }
    @Override
    protected void onResume() {
        super.onResume();
        // Tweetプレビュー情報表示
        setTweetPreview();
    }

    public void onSosClick(View v) {
        // TextView txt = (TextView) findViewById(R.id.main_txt_tweet_preview);
        String name = ((EditText) findViewById(R.id.main_txt_name)).getText().toString();
        mPref.set("name", name);
        // 緯度経度が取得できていればプリファレンス登録
        if (mLat != null && mLon != null) {
            mPref.set("lat", mLat);
            mPref.set("lon", mLon);
        }

        Intent intent = WidgetProvider.getLaunchIntent(getApplicationContext(), -1, 0);
        sendBroadcast(intent);
    }

    public void onSettingClick(View v) {
        Intent intent = new Intent(getApplicationContext(), SettingActivity.class);
        startActivity(intent);
    }

    private void setTweetPreview() {
        if (mTemplateTweetMes == null) {
            // 定型文
            String curentPatternMes = "";
            String currentPatternId = mPref.get("current_pattern");
            if ("".equals(currentPatternId)) {
                currentPatternId = "0";
                curentPatternMes = getText(R.string.preset1).toString();
            } else {
                curentPatternMes = mPref.get("pattern" + currentPatternId);
            }
            Log.i("SOS", "curentPatternMes=" + curentPatternMes);

            // ハッシュタグ
            String hashtag = mPref.get("hashtag");
            if ("".equals(hashtag)) {
                hashtag = getText(R.string.presethashtag).toString();
            }
            hashtag = " #" + hashtag;

            mTemplateTweetMes = curentPatternMes + hashtag;
        }

        // 名前
        EditText edit = (EditText) findViewById(R.id.main_txt_name);
        String name = edit.getText().toString();
        if (!"".equals(name)) {
            name += "：";
        }

        // メッセージ連結
        String mes = name + mTemplateTweetMes;

        // TweetPreview更新
        TextView txt = (TextView) findViewById(R.id.main_txt_tweet_preview);
        txt.setText(mes);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            setTweetPreview();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        mTemplateTweetMes = null;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}